<template>
  <div id="page-material">
    <!-- 查询 -->
    <div class="page-search-list">
      <el-form label-position="left" :model="search_from" :inline="true">
        <el-form-item
          v-for="(item, i) in search_info"
          :label="item.label"
          :key="i"
          style="margin-bottom: 5px;"
        >
          <template v-if="item.prop === 'time'">
            <template v-if="!is_time_period">
              <el-date-picker
                type="dates"
                v-model="search_from.time"
                placeholder="选择一个或多个日期"
                key="time"
                value-format="yyyy-MM-dd"
                :append-to-body="false"
                size="small"
              ></el-date-picker>
            </template>
            <template v-else>
              <el-date-picker
                v-model="search_from.time_period"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                key="time_period"
                value-format="yyyy-MM-dd"
                :append-to-body="false"
                size="small"
              ></el-date-picker>
            </template>
          </template>
          <template v-else-if="item.prop === 'cid'">
            <select-community
              :cid.sync="search_from[item.prop]"
              :pid.sync="search_from.pid"
              :bid.sync="search_from.bid"
              :sidVisiabel="false"
            ></select-community>
          </template>
          <template v-else-if="item.prop === 'type'">
            <el-select
              v-model="search_from[item.prop]"
              multiple
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option
                v-for="item in material_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <template v-else-if="item.prop === 'status'">
            <el-select
              v-model="search_from[item.prop]"
              multiple
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option
                v-for="item in material_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <template v-else>
            <el-input
              v-model="search_from[item.prop]"
              size="small"
              :placeholder="`请输入${item.label}`"
            ></el-input>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button class="el-icon-search" size="small" @click.native="search">
            搜索
          </el-button>
          <el-button
            type="primary"
            class="el-icon-refresh"
            size="small"
            @click.native="resetSearchFields"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="page-botton-list">
      <el-button
        plain
        class="el-icon-date"
        size="mini"
        @click.native="is_time_period = !is_time_period"
      >
        {{ is_time_period ? '时间查询' : '时间段查询' }}
      </el-button>
      <el-button
        type="warning"
        plain
        class="el-icon-plus"
        size="mini"
        @click="add_material_dialog = true"
      >
        新增
      </el-button>
      <el-button
        type="danger"
        plain
        class="el-icon-delete"
        size="mini"
        :disabled="is_delete"
        @click="deleteMaterial"
        key="delete_arr_button"
      >
        删除
      </el-button>
      <ExportExcel
        style="margin-left: 0.625rem;"
        :id="'material_tableId'"
        :label="'导出'"
        :multipleSelection="multiple_selection"
        :tableData="material_table"
        :tableInfo="material_tableInfo"
      ></ExportExcel>
    </div>
    <!-- 表格 -->
    <div class="page-table">
      <el-table
        ref="material_tableRef"
        :data="material_table"
        style="width: 100%;"
        id="material_tableId"
        @selection-change="selectTabel"
      >
        <el-table-column type="selection" />
        <el-table-column
          v-for="(column, i) in material_tableInfo"
          :key="i"
          :label="column.label"
          align="center"
        >
          <template slot-scope="scope">
            <template v-if="column.prop === 'type'">
              <el-tag v-if="scope.row[column.prop] == 2">防疫用品</el-tag>
              <el-tag v-else-if="scope.row[column.prop] == 3" type="success">
                食品
              </el-tag>
              <el-tag v-else-if="scope.row[column.prop] == 0" type="warning">
                生活用品
              </el-tag>
              <el-tag v-else-if="scope.row[column.prop] == 1">医用药品</el-tag>
              <el-tag v-else type="info">其他</el-tag>
            </template>
            <template v-else-if="column.prop === 'status'">
              <el-tag v-if="scope.row[column.prop] == 0">充足</el-tag>
              <el-tag v-else-if="scope.row[column.prop] == 1" type="danger">
                缺货
              </el-tag>
              <el-tag v-else-if="scope.row[column.prop] == 3" type="warning">
                其他
              </el-tag>
              <el-tag v-else type="info">失效</el-tag>
            </template>
            <template v-else>{{ scope.row[column.prop] }}</template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="mini"
              class="el-icon-edit"
              @click="changeMaterial(scope.row)"
            >
              修改
            </el-button>
            <el-button
              type="warning"
              plain
              size="mini"
              class="el-icon-delete"
              @click="deleteMaterial(scope.row, true)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageCount"
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tatal"
        class="upload-list-pagination"
      ></el-pagination>
    </div>

    <!-- 添加/修改 弹窗 -->
    <el-dialog
      :title="is_change_travel ? '修改物资信息' : '添加物资信息'"
      :visible.sync="add_material_dialog"
      :append-to-body="true"
      width="40%"
    >
      <el-form
        :model="dialog_from"
        label-position="left"
        :rules="rules"
        ref="addMaterialRef"
      >
        <el-form-item
          v-for="(item, i) in dialog_info"
          :key="i"
          :label="item.label"
          :prop="item.prop"
          label-width="80px"
        >
          <template v-if="item.prop === 'cid'">
            <select-community
              :cid.sync="dialog_from[item.prop]"
              :pid.sync="dialog_from.pid"
              :bid.sync="dialog_from.bid"
              :sidVisiabel="false"
            ></select-community>
          </template>
          <template v-else-if="item.prop === 'type'">
            <el-select
              v-model="dialog_from[item.prop]"
              multiple
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option
                v-for="item in material_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <template v-else-if="item.prop === 'status'">
            <el-select
              v-model="dialog_from[item.prop]"
              multiple
              placeholder="请选择"
              clearable
              size="small"
            >
              <el-option
                v-for="item in material_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <template v-else>
            <el-input v-model="dialog_from[item.prop]" size="small"></el-input>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button
          type="primary"
          v-if="is_change_travel"
          @click="uploadChangeTravel"
        >
          确定修改
        </el-button>
        <el-button v-else type="primary" @click="addClick">
          确定添加
        </el-button>
      </div>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="delete_dialog_visible"
      width="30%"
      center
      :append-to-body="true"
    >
      <div style="text-align: center;">
        是否删除以下物资所有信息
        <li v-for="(item, i) in delete_material_arr" :key="i">
          {{ i + 1 + '.' + item.name + '（' + item.id + '）' }}
        </li>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="falsedeleteMaterial">取 消</el-button>
        <el-button type="primary" @click="truedeleteMaterial">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { rules } from '@/utils/rules'
// API
import { getCommunityList } from '@/api/community/'
import { getTravelList } from '@/api/admin/travel'
// 组件
import ExportExcel from '@/components/ExportExcel/'
// 工具类
import { assignAttr } from '@/utils/tool.js'
export default {
  components: { ExportExcel },
  computed: {
    is_delete() {
      return !this.multiple_selection.length > 0
    },
  },
  watch: {
    search_from: {
      handler(val) {
        console.debug(val)
      },
    },
    add_material_dialog: {
      handler(val) {
        if (!val) {
          if (this.$refs.addMaterialRef)
            this.$refs.addMaterialRef.clearValidate()
          this.dialog_from = {}
        }
      },
    },
    delete_dialog_visible: {
      handler(val) {
        if (!val) {
          this.delete_material_arr = []
          this.$refs.material_tableRef.clearSelection()
        }
      },
    },
    dialog_from: {
      handler(val) {},
      deep: true,
      immediate: true,
    },
  },
  data() {
    return {
      tatal: 10, //总数据
      pageCount: 1, // 页数
      pageSize: 10, // 页的大小
      // 查询信息
      search_from: {
        cid: '',
        pid: '',
        bid: '',
        name: '', // 物资名称
        type: '', //根据物资类型
        status: '', //根据物资状态
        time: [], // 选择时间 可多选
        time_period: [], // 时间段
        admin: '', // 管理员
      },
      search_info: {
        cid: { label: '社区', prop: 'cid' },
        admin: { label: '录入员', prop: 'admin' },
        time: { label: '录入日期', prop: 'start_time' },
        type: { label: '物资类型', prop: 'type' },
        status: { label: '物资状态', prop: 'status' },
      },
      material_type: [
        { value: 0, label: '生活用品' },
        { value: 1, label: '医用药品' },
        { value: 2, label: '防疫用品' },
        { value: 3, label: '食品' },
        { value: 4, label: '其他' },
      ],
      material_status: [
        { value: 0, label: '充足' },
        { value: 1, label: '缺货' },
        { value: 2, label: '失效' },
        { value: 3, label: '其他' },
      ],
      is_time_period: false, // 是否以时间段查询
      material_tableInfo: {
        id: { label: '物资编号', prop: 'id' },
        cid: { label: '社区', prop: 'cid' },
        pid: { label: '期', prop: 'pid' },
        bid: { label: '栋', prop: 'bid' },
        name: { label: '物资名称', prop: 'name' },
        type: { label: '物资类型', prop: 'type' },
        count: { label: '数量', prop: 'count' },
        status: { label: '物资状态', prop: 'status' },
        admin: { label: '录入员', prop: 'admin' },
        time: { label: '录入时间', prop: 'time' },
      },
      material_table: [
        {
          id: 4206,
          cid: '滨海小区',
          pid: '1期',
          bid: '3栋',
          name: '口罩',
          count: '500个',
          type: 2,
          status: 0,
          admin: '管理员1',
          time: '2022-5-6',
        },
        {
          id: 4205,
          cid: '滨海小区',
          pid: '2期',
          bid: '3栋',
          name: '酒精',
          count: '500L',
          type: 1,
          status: 0,
          admin: '管理员2',
          time: '2022-5-6',
        },
      ],
      multiple_selection: [], // 表格选择
      add_material_dialog: false, // 弹窗是否可见
      is_change_travel: false, // 是否修改
      dialog_from: {
        cid: '',
        pid: '',
        bid: '',
        name: '',
        status: '',
        type: '',
        count: '',
      },
      dialog_info: {
        cid: { label: '社区', prop: 'cid' },
        name: { label: '物资名称', prop: 'name' },
        count: { label: '数量', prop: 'count' },
        type: { label: '物资类型', prop: 'type' },
        status: { label: '物资状态', prop: 'status' },
      },
      pass_address_arr: [], // 存储途径地
      // 单个途径地对象
      temppass_address: {
        id: '',
        time: '',
        address: '',
      },
      delete_dialog_visible: false, //删除弹窗是否可见
      delete_material_arr: [], // 删除数组
      all_material_form: {
        title: '',
        cid: '',
        content: '',
      },
      rules: {
        ...rules,
        count: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        status: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        // type: [{ required: true, message: '请选择类型', prop: 'blur' }],
      },
    }
  },
  created() {},
  methods: {
    /* 请求 */
    /* DOM 操作 */
    // 搜索
    search() {
      console.debug(this.search_from)
      const data = this.travelFrm
    },
    // 清空搜索条件
    resetSearchFields() {
      this.search_from = {
        cid: '',
        uname: '',
        time: [],
        time_period: [],
        admin_id: '',
      }
    },
    // 表格选择
    selectTabel(val) {
      this.multiple_selection = val
    },

    addAllClick() {},

    addClick() {
      this.dialog_from.pass_address = JSON.stringify(this.pass_address_arr)
      this.add_material_dialog = false
      this.$message({
        message: '添加成功',
        type: 'success',
      })
    },
    // 点击 取消 最外弹窗
    cancelDialog() {
      this.add_material_dialog = false
      this.pass_address_arr = [] // 清空途径数组
    },
    // 点击修改
    changeMaterial(row) {
      this.add_material_dialog = true
      this.is_change_travel = true
      // 查询居民行程
      console.debug(row)
      assignAttr(row, this.dialog_from)
    },
    // 上传修改的行程
    uploadChangeTravel() {
      this.add_material_dialog = false
      this.$message({
        message: '修改成功',
        type: 'success',
      })
    },
    // 删除行程
    deleteMaterial(row, flag) {
      console.log(row)
      this.delete_dialog_visible = true
      if (flag) {
        // 单个删除
        let material = {
          id: row.id,
          name: row.name,
        }
        this.delete_material_arr.push(material)
        console.debug('删除单个')
      } else {
        console.log()
        this.multiple_selection.forEach((item) => {
          let material = {
            id: item.id,
            name: item.name,
          }
          this.delete_material_arr.push(material)
        })
        console.debug('删除数组')
      }
      console.debug(this.delete_material_arr)
    },
    falsedeleteMaterial() {
      this.delete_dialog_visible = false
      this.$message('已取消删除')
    },
    truedeleteMaterial() {
      this.delete_dialog_visible = false
      this.$message({
        message: '删除成功',
        type: 'success',
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
  },
}
</script>

<style lang="less" scoped>
#page-material {
  padding: 0 0.5rem;
  .page-table {
    .el-button {
      margin: 2px 0;
    }
  }
  .el-collapse-title-div {
    display: flex;
    justify-content: space-around;
    width: 90%;
  }
  .el-form-ul {
    min-height: 2.375rem;
    border: 1px solid #dcdfe6;
  }
  /deep/.el-collapse {
    border: none;
    background-color: rgba(255, 255, 255, 0);
  }
  /deep/.el-collapse-item__content {
    padding-bottom: 0;
  }
}
.page-botton-list {
  margin-bottom: 0.625rem;
}
.ul-address-date {
  display: flex;
  .ul-address-date-item {
    flex: 1;
    margin-right: 1.25rem;
    &:last-child {
      margin-right: 0;
    }
  }
}
/deep/.el-select {
  width: 100%;
}
.address-tag {
  margin-right: 0.625rem;
  &:last-child {
    margin-right: 0;
  }
}
.upload-list-pagination {
  margin-top: 1.8rem;
  display: flex;
  justify-content: center;
}
</style>
